package l1j.server.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.DatabaseFactory;
import l1j.server.server.model.shop.L1Shop;
import l1j.server.server.templates.L1ShopItem;
import l1j.server.server.utils.SQLUtil;

public class ShopTable {

	private static Logger _log = Logger.getLogger(ShopTable.class.getName());

	private static ShopTable _instance;

	// TODO 新所有物品販賣 by eric1300460
	public static ShopTable getInstance() {
		if (_instance == null) {
			_instance = new ShopTable();
		}
		return _instance;
	}

	private final HashMap<Integer, L1Shop> _allShops = new HashMap<Integer, L1Shop>();

	// TODO 新所有物品販賣 by eric1300460
	private final HashMap<Integer, Integer> _allItemSells = new HashMap<Integer, Integer>();
	// TODO 新所有物品販賣 by eric1300460

	public ShopTable() {
		this.loadShopAllSell();
		this.loadShops();
	}
	// TODO 新增所有物品販賣 by eric1300460

	private ArrayList<Integer> enumNpcIds() {
		final ArrayList<Integer> ids = new ArrayList<Integer>();

		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT DISTINCT npc_id FROM shop");
			rs = pstm.executeQuery();
			while (rs.next()) {
				ids.add(rs.getInt("npc_id"));
			}
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs, pstm, con);
		}
		return ids;
	}

	// TODO 新所有物品販賣 by eric1300460
	public L1Shop get(final int npcId) {
		return this._allShops.get(npcId);
	}

	// TODO 修正新所有東西販賣

	// TODO 新所有物品販賣 by eric1300460
	public int getItemPrice(final int id) {
		if (this._allItemSells.get(id) == null) {
			return 0;
		} else {
			return this._allItemSells.get(id);
		}
	}

	private L1Shop loadShop(final int npcId, final ResultSet rs) throws SQLException {
		final List<L1ShopItem> sellingList = new ArrayList<L1ShopItem>();
		final List<L1ShopItem> purchasingList = new ArrayList<L1ShopItem>();
		while (rs.next()) {
			final int itemId = rs.getInt("item_id");
			final int sellingPrice = rs.getInt("selling_price");
			final int purchasingPrice = rs.getInt("purchasing_price");
			int packCount = rs.getInt("pack_count");
			packCount = packCount == 0 ? 1 : packCount;
			/* if (0 <= sellingPrice) {//完整的
			L1ShopItem item = new L1ShopItem(itemId, sellingPrice,
					packCount);
			sellingList.add(item);
		}
		if (0 <= purchasingPrice) {
			L1ShopItem item = new L1ShopItem(itemId, purchasingPrice,
					packCount);
			purchasingList.add(item);
		}//元寶商人
	  }*/
			final int yuanbao = rs.getInt("推文");//add eboy
			final int gash = rs.getInt("商城");//opqlo 商城
			final int check = rs.getInt("支票");//opqlo 支票
			final int other1 = rs.getInt("其他1");//opqlo 其他貨幣1
			final int other2 = rs.getInt("積分");//opqlo 其他貨幣2
			final int deleteDay = rs.getInt("delete_day"); // 道具天數刪除系統(指定天數)
			final Timestamp deleteDate = rs.getTimestamp("delete_date"); // 道具天數刪除系統(指定日期)
			final int RemainingTime = rs.getInt("MaxUseTime");//opqlo 使用時間限制
			switch(npcId){
			case 70068:case 70020:
			case 70024:case 70032:
			case 80075:
				continue;
			default:
				break;
			}
			if (sellingPrice >=0 &&  yuanbao <=0 && gash<=0 &&check <=0 && other1 <=0 && other2 <=0) {
				final L1ShopItem item = new L1ShopItem(itemId, sellingPrice, packCount, deleteDay, deleteDate ,RemainingTime); // 道具天數刪除系統
				sellingList.add(item);

			}else if (purchasingPrice >=0 && yuanbao <=0 && gash<=0 && check<=0 && other1 <=0 && other2 <=0) {
				final L1ShopItem item = new L1ShopItem(itemId, purchasingPrice,packCount, deleteDay, deleteDate,RemainingTime); // 道具天數刪除系統
				purchasingList.add(item);

			}else if (yuanbao > 0 && gash<=0 && check<=0 && other1 <=0 && other2 <=0) { //add eboy
				final L1ShopItem item = new L1ShopItem(itemId, yuanbao,packCount, deleteDay, deleteDate,RemainingTime); // 道具天數刪除系統
				sellingList.add(item); //add eboy end //元寶商人-永恆新增


			}else if(gash>0 && yuanbao <=0 && check<=0 && other1 <=0 && other2 <=0){//opqlo 商城
				final L1ShopItem item = new L1ShopItem(itemId, gash,packCount, deleteDay, deleteDate,RemainingTime); // 道具天數刪除系統
				sellingList.add(item);

			}else if(check>0 && yuanbao <=0 && gash <=0 && other1 <=0 && other2 <=0){//opqlo 支票
				final L1ShopItem item = new L1ShopItem(itemId, check,packCount, deleteDay, deleteDate,RemainingTime); // 道具天數刪除系統
				sellingList.add(item);

			}else if(other1>0 && yuanbao <=0 && gash <=0 && check <=0 && other2 <=0){//opqlo 其他貨幣1
				final L1ShopItem item = new L1ShopItem(itemId, other1,packCount, deleteDay, deleteDate,RemainingTime); // 道具天數刪除系統
				sellingList.add(item);


			}else if(other2>0 && yuanbao <=0 && gash <=0 && check <=0 && other1 <=0){//opqlo 其他貨幣2
				final L1ShopItem item = new L1ShopItem(itemId, other2,packCount, deleteDay, deleteDate,RemainingTime); // 道具天數刪除系統
				sellingList.add(item);
			}
		}
		return new L1Shop(npcId, sellingList, purchasingList);
	}

	// TODO 修正新所有東西販賣
	private void loadShopAllSell() {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM shop_sell_price ORDER BY item_id");
			rs = pstm.executeQuery();
			while (rs.next()) {
				final int itemId = rs.getInt("item_id");
				// String name = rs.getString("name");
				final int price = rs.getInt("sell_price");
				if (price >= 1) {
					this._allItemSells.put(itemId, price);
				}
			}
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs, pstm, con);
		}
	}

	private void loadShops() {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM shop WHERE npc_id=? ORDER BY order_id");
			for (final int npcId : this.enumNpcIds()) {
				pstm.setInt(1, npcId);
				rs = pstm.executeQuery();
				final L1Shop shop = this.loadShop(npcId, rs);
				this._allShops.put(npcId, shop);
				rs.close();
			}
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs, pstm, con);
		}
	}
}
